using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace PhilWicklund.com.FacebookParts
{
    public class LoginEditorPart : EditorPart
    {
        DropDownList sizes;
        DropDownList lengths;
        CheckBox showLogout;
        DropDownList orientation;

        protected override void CreateChildControls()
        {
            Controls.Add(new LiteralControl("Button Size:<br />"));
            sizes = new DropDownList();
            sizes.Items.Add(LoginPart.Sizes.small.ToString());
            sizes.Items.Add(LoginPart.Sizes.medium.ToString());
            sizes.Items.Add(LoginPart.Sizes.large.ToString());
            this.Controls.Add(sizes);

            this.Controls.Add(new LiteralControl("<br /><br />"));

            Controls.Add(new LiteralControl("Button Length:<br />"));
            lengths = new DropDownList();
            lengths.Items.Add(LoginPart.Lengths.Shorter.ToString());
            lengths.Items.Add(LoginPart.Lengths.Longer.ToString());
            this.Controls.Add(lengths);

            this.Controls.Add(new LiteralControl("<br /><br />"));

            Controls.Add(new LiteralControl("Show Logout Button:<br />"));
            showLogout = new CheckBox();
            this.Controls.Add(showLogout);

            this.Controls.Add(new LiteralControl("<br /><br />"));

            Controls.Add(new LiteralControl("Orientation:<br />"));
            orientation = new DropDownList();
            orientation.Items.Add(Orientations.left.ToString());
            orientation.Items.Add(Orientations.right.ToString());
            orientation.Items.Add(Orientations.center.ToString());
            this.Controls.Add(orientation);
        }

        public override void SyncChanges()
        {
            this.EnsureChildControls();
            LoginPart sourcePart = (LoginPart)this.WebPartToEdit;
            string SelectedSize = sourcePart.ButtonSize.ToString();
            string SelectedLength = sourcePart.ButtonLength.ToString();
            sizes.Items.FindByText(SelectedSize).Selected = true;
            lengths.Items.FindByText(SelectedLength).Selected = true;
            showLogout.Checked = sourcePart.AutoLogoutLink;

            string SelectedOrientation = sourcePart.Orientation.ToString();
            orientation.Items.FindByText(SelectedOrientation).Selected = true;
        }

        public override bool ApplyChanges()
        {
            this.EnsureChildControls();
            LoginPart targetPart = (LoginPart)this.WebPartToEdit;
            switch (sizes.SelectedItem.Text)
            {
                case "small": targetPart.ButtonSize = LoginPart.Sizes.small; break;
                case "medium": targetPart.ButtonSize = LoginPart.Sizes.medium; break;
                case "large": targetPart.ButtonSize = LoginPart.Sizes.large; break;
            }

            switch (lengths.SelectedItem.Text)
            {
                case "Shorter": targetPart.ButtonLength = LoginPart.Lengths.Shorter; break;
                case "Longer": targetPart.ButtonLength = LoginPart.Lengths.Longer; break;
            }

            switch (orientation.SelectedItem.Text)
            {
                case "left": targetPart.Orientation = Orientations.left; break;
                case "right": targetPart.Orientation = Orientations.right; break;
                case "center": targetPart.Orientation = Orientations.center; break;
            }
            targetPart.AutoLogoutLink = showLogout.Checked;
            return true;
        }
    }
}
